package com.op.studyingliteapp.database.dao;

import java.util.ArrayList;
import java.util.List;

import com.op.studyingliteapp.bean.BaseBean;
import com.op.studyingliteapp.database.DBHelper;
import com.op.studyingliteapp.database.MainDBHelper;
import com.op.studyingliteapp.database.table.BaseColumn;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

/**
 * @author handsome
 * @version 创建时间：2015年7月25日 下午12:03:16 类说明
 */
public class AbstractDao {
	protected String tb;
	protected SQLiteDatabase db;

	public AbstractDao(Context context) {
		this.db = new MainDBHelper(context).getWritableDatabase();
	}

	public AbstractDao(Context context,String userId) {
		this.db = new DBHelper(context,userId).getWritableDatabase();
	}

	/**
	 * insert方法
	 */
	public long insert(BaseBean baseBean) {
		return db.insert(tb, null, baseBean.getContentValues());
	}

	/**
	 * update
	 */
	public long update(BaseBean baseBean) {
		String where;
		String id = baseBean.getId();
		ContentValues contentValues = baseBean.getContentValues();
		try {
			where = BaseColumn.COLUMN_ID + " = ?";
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}
		return db.update(tb, contentValues, where, new String[] { id });
	}

	/**
	 * delete方法
	 */
	public long delete(BaseBean baseBean) {
		String where;
		String id = baseBean.getId();
		try {
			where = BaseColumn.COLUMN_ID + " = ?";
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}
		return db.delete(tb, where, new String[] { id });
	}

	/**
	 * 批量delete
	 */
	public long delete(BaseBean... baseBeans) {
		this.db.beginTransaction();
		try {
			for (BaseBean baseBean : baseBeans) {
				this.delete(baseBean);
			}
			this.db.setTransactionSuccessful();
			this.db.endTransaction();
			return 0;
		} catch (Exception e) {
			e.printStackTrace();
			this.db.endTransaction();
			return -1;
		}
	}

	/**
	 * 查找所有的ids
	 */
	public List<String> getIds() {
		List<String> ids = new ArrayList<String>();
		String[] columns = { BaseColumn.COLUMN_ID };
		Cursor cursor = db.query(this.tb, columns, null, null, null, null, null);
		while (cursor.moveToNext()) {
			ids.add(cursor.getString(cursor.getColumnIndex(BaseColumn.COLUMN_ID)));
		}
		return ids;
	}
	/**
	 * 清空方法
	 * 
	 * @return
	 */
	public long empty() {
		try {
			String sql = "delete from " + tb + ";";
			db.execSQL(sql);
			return 0;
		} catch (Exception e) {
			e.printStackTrace();
			this.db.endTransaction();
			return -1;
		}
	}

	/**
	 * 插入或者更新
	 * 
	 * @return
	 */
	public long insertOrUpdate(BaseBean baseBean) {
		try {
			List<String> ids = getIds();
			if (ids.contains(baseBean.getId())) {
				update(baseBean);
			} else {
				insert(baseBean);
			}

			return 0;
		} catch (Exception e) {
			e.printStackTrace();
			this.db.endTransaction();
			return -1;
		}
	}

}
